const Promise = require('bluebird');
const moment = require('moment');

/**
 * @api {get} /api/admin/v1/searchs/browse 用户搜索表
 */
exports.browse = function ({query}, {models: {UsersSearchs, Users, associations}, modelsData: {Goodses, RemoveGoodses}, cache: {settings}, controller}) {
    let pagination = controller.pagination({
        params: query,
        filters: ['goods_id', 'user_id', 'agency_id'],
        likes: ['keyword', 'goods_id', 'user_id']
    });
    return UsersSearchs.findAndCountAll(pagination).then(function (data) {
        return associations.relateOne(data, Users, 'user_id', 'user', ['id', 'image', 'nickname', 'username', 'nickname', 'tel']);
    }).then(function (data) {
        return associations.relateOne(data, Users, 'agency_id', 'agency', ['id', 'image', 'nickname', 'username', 'nickname', 'tel']);
    }).then(function (data) {
        let rows = data.rows, ids = [];
        if (rows && rows.length) {
            for (let i in rows) {
                let row = rows[i];
                if (row.goods_id) {
                    ids.push(row.goods_id);
                }
            }
            if (ids.length) {
                return Goodses.findAll({
                    where: {auctionId: ids},
                    attributes: ['id', 'pictUrl', 'title', 'auctionId', 'zkPrice', 'quanAmount', 'quanPrice', 'tkCommFee', 'eventFee'],
                    raw: true
                }).then(function (goodses) {
                    let map = {};
                    for (let i in goodses) {
                        let goods = goodses[i];
                        map[goods.auctionId] = goods;
                    }
                    for (let i in rows) {
                        let row = rows[i];
                        if (map[row.goods_id]) {
                            row.goods = map[row.goods_id];
                        }
                    }
                    return data;
                });
            }
        }
        return data;
    }).then(function (data) {
        let rows = data.rows, ids = [];
        if (rows && rows.length) {
            for (let i in rows) {
                let row = rows[i];
                if (row.goods_id && !row.goods) {
                    ids.push(row.goods_id);
                }
            }
            if (ids.length) {
                return RemoveGoodses.findAll({
                    where: {auctionId: ids},
                    attributes: ['id', 'pictUrl', 'title', 'auctionId', 'zkPrice', 'quanAmount', 'quanPrice', 'tkCommFee', 'eventFee'],
                    raw: true
                }).then(function (goodses) {
                    let map = {};
                    for (let i in goodses) {
                        let goods = goodses[i];
                        map[goods.auctionId] = goods;
                    }
                    for (let i in rows) {
                        let row = rows[i];
                        if (map[row.goods_id]) {
                            row.goods_remove = true;
                            row.goods = map[row.goods_id];
                        }
                    }
                    return data;
                });
            }
        }
        return data;
    });
}